<?php
     // error_reporting(E_ALL);
 
     session_start();
    include_once './includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    db_query ("INSERT into  quote_log  (log) VALUES ('%s')", "test");

    $file =  basename($_FILES['uploadfile']['name']);
    
   db_query ("INSERT into  quote_log  (log) VALUES ('%s')", $file);
   $insertID = NULL;
   if (isset ($_SESSION['insertId']))
   { 
      $insertID = $_SESSION['insertId'];
   }
   if ( ! $insertID ) {
        db_query ("INSERT into  quote_log  (log) VALUES ('%s')", "no id");
          
        $uid = $_REQUEST['uid'];
        $comment = $_REQUEST['comment'];
        //$sql = "---INSERT INTO quote_info(uid, comment) VALUES ($uid, '$comment')";
        //db_query("INSERT into  quote_log  (log) VALUES ('%s')", $sql);
        if ($uid ) 
        {
        	$sql = "INSERT INTO quote_info(uid, comment) VALUES ($uid, '$comment')";
        	//db_query("INSERT into  quote_log  (log) VALUES ('%s')", $sql);
            db_query ("INSERT INTO quote_info(uid, comment) VALUES ($uid, '$comment')");
        } else 
        {
            $contact_name = $_REQUEST['contact_name'];
            $company_name = $_REQUEST['company_name'];
            $email = $_REQUEST['email'];
            $phone = $_REQUEST['phone'];
            db_query ("INSERT INTO quote_info(contact_name, company_name, email, phone,  comment) VALUES ('%s','%s', '%s', '%s',  '%s')",
            $contact_name, $company_name, $email, $phone,  $comment);
        }
        $r = db_query("SELECT last_insert_id() as id");
       if (  /*$data = db_fetch_object($r)*/1 ) 
       {
            db_query ("INSERT into  quote_log  (log) VALUES ('%s')", "new id ");
            $data = db_fetch_object($r);
            $_SESSION['insertId'] = $data->id;
            db_query ("INSERT into  quote_log  (log) VALUES ('%d')",  $data->id);
       }
        else { 
          db_query ("INSERT into  quote_log  (log) VALUES ('%s')",  "didn't get new id");
      }
    }
    else  { 
       db_query ("INSERT into  quote_log  (log) VALUES ('%s')",    'get id');
   }
    $insertID =  $_SESSION['insertId'];
    
    $sql = "INSERT INTO quote_file (info_id, file_name) VALUES ($insertID, '$file')";
    db_query ("INSERT into  quote_log  (log) VALUES ('%s')",  $sql);
    db_query ($sql);
    $r = db_query("SELECT last_insert_id() as id");
    $file_id = 0;
    if ($data = db_fetch_object($r) ) 
     {
          
            //$data = db_fetch_object($r);
            $file_id = $data->id;     
              db_query ("INSERT into  quote_log  (log) VALUES ('%s')", "new id: $file_id ");
     }
 //$name = $_REQUEST['email];
 //fwrite($h, "company name =".$name);
 

    $uploaddir = '/home/zwu/app/re-eng/files/'; 
    $file = $uploaddir . $file_id.'_'.basename($_FILES['uploadfile']['name']); 
    $size=$_FILES['uploadfile']['size'];
    $tempFile  = $_FILES['uploadfile']['tmp_name'];
    if($size>1024*1024*20)
    {
	echo "error file size > 20 MB";
	unlink($_FILES['uploadfile']['tmp_name']);
	exit;
    }
    if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
       echo "success"; 
    } else {
	$error = "error ".$_FILES['uploadfile']['error']." --- ".$_FILES['uploadfile']['tmp_name']." %%% ".$file."($size)";
        db_query ("INSERT into quote_info  (log) VALUES('$error')");
    }
?>
